<!doctype html>
<html>

  <head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/mui.min.css" rel="stylesheet" />
    <link href="../css/1px.css" rel="stylesheet" />
    <link href="../css/weui.min.css" rel="stylesheet" />
    <style>
      html {
        font-family: Roboto, Arial, "Noto Sans CJK SC", "Noto Sans CJK TC", "Pingfang SC", "Microsoft Yahei", "WenQuanYi Micro Hei", sans-serif;
      }
      
      body {
        overflow-x: hidden;
        background-color: #fff !important;
      }
      
      .receive {
        font-size: 14px;
        box-sizing: border-box;
      }
      
      .receive-option {
        background-color: #fff;
        text-align: center;
        padding: 20px 0;
        margin: 10px;
        border: 1px dashed #ccc;
        border-radius: 5px;
      }
    </style>
  </head>

  <body>
    <script src="../js/mui.min.js"></script>
    <script type="text/javascript">
      mui.init();
    </script>

    <div class="receive">
      <div class="receive-select weui-flex">
        <div class="receive-option weui-flex__item" id="store">
          总仓库发货
          <div><span id="store-receive">查询中...</span>单</div>
        </div>
        <div class="receive-option weui-flex__item" id="shop">
          其他门店发货
          <div><span id="shop-receive">查询中...</span>单</div>
        </div>
      </div>

      <script src="../config.js" type="text/javascript" charset="utf-8"></script>
      <script type="text/javascript">
        var originData = {
          currentShopObj: '',
          fromTime: '',
          toTime: '',
          storeReceiveList: [],
          shopReceiveList: [],
          shopObjs: [],
          ApplyStatusKeyValue: []
        };

        mui.plusReady(function() {
          receiveParams();
          checkRange();
          bindEvent();
        });

        function receiveParams() {
          var self = plus.webview.currentWebview();
          originData.currentShopObj = self.currentShopObj;
          originData.shopObjs = self.shopObjs;
        }

        function checkRange() {
          config.ajax({
            url: config.basePath + '/apply/checkrange',
            headers: {
              'Content-Type': 'application/json'
            },
            crossDomain: true,
            dataType: 'json',
            type: 'GET',
            success: function(res) {
              if(res.result === 1) {
                computedTime(res.startDate, res.endDate);
                getStoreCount();
                getShopCount();
              }
            }
          });
        }

        function computedTime(startDate, endDate) {
          var strFrom = (startDate + '').substring(0, 4) + '-' + (startDate + '').substring(4, 6) + '-01 00:00:00';
          var strTo = new Date((endDate + '').substring(0, 4), (endDate + '').substring(4, 6)).toJSON().substring(0, 10) + " 23:59:59";
          originData.fromTime = new Date(strFrom).getTime();
          originData.toTime = new Date(strTo).getTime();
        }

        function getStoreCount() {
          var storeReceiveList = []
          var totalCount = 0
          //误差 已驳回
            config.ajax({
              url: '/delivery/checkoperation',
              data: {
                fromTime: originData.fromTime,
                toTime: originData.toTime,
                operationStatus: 5,
                storeId: originData.currentShopObj.storeId,
                pageNo: 1,
                pageCount: 100
              },
              success: function(res) {
                if(res.result === 1) {
                  totalCount += res.operations.totalCount
                  storeReceiveList = res.operations.data
                  originData.storeReceiveList = storeReceiveList
                   mui('#store-receive')[0].innerHTML = totalCount;
                   noticeReceive();
                }
              }
            })
        }

        function noticeReceive() {
          var receive = plus.webview.getWebviewById('store-receive');
          if(receive) {
            mui.fire(receive, 'receiveParams', {
              currentShopObj: originData.currentShopObj,
              fromTime: originData.fromTime,
              toTime: originData.toTime,
              storeReceiveList: originData.storeReceiveList,
              shopObjs: originData.shopObjs
            });
          }
        }

        function noticeStore() {
          var shopReceive = plus.webview.getWebviewById('shop-receive');
          if(shopReceive) {
            mui.fire(shopReceive, 'receiveParams', {
              currentShopObj: originData.currentShopObj,
              fromTime: originData.fromTime,
              toTime: originData.toTime,
              shopReceiveList: originData.shopReceiveList,
              shopObjs: originData.shopObjs
            });
          }
        }

        function getShopCount() {
          config.ajax({
            url: config.basePath + '/conversion/checkconversion.do',
            headers: {
              'Content-Type': 'application/json'
            },
            crossDomain: true,
            dataType: 'json',
            type: 'POST',
            data: {
              recivedStatus: 1, //待收货的
              storeId: originData.currentShopObj.storeId,
              fromTime: originData.fromTime,
              toTime: originData.toTime
            },
            success: function(res) {
              if(res.result === 1) {
                mui('#shop-receive')[0].innerHTML = res.conversions.conversions.totalCount;
                originData.shopReceiveList = res.conversions.conversions.data;
                noticeStore();
              }
            }
          });
        }

        function bindEvent() {
          window.addEventListener('refresh', function() {
            checkRange();
          });
          mui('.receive-select').on('tap', '#store', function() {
            mui.openWindow({
              url: 'store-receive.html',
              id: 'store-receive',
              styles: {
                titleNView: {
                  titleText: originData.currentShopObj.storeName + '收货',
                  titleColor: "#FFFFFF",
                  titleSize: "17px",
                  backgroundColor: "#FF6800",
                  autoBackButton: true,
                  buttons: [{
                    fontWeight: 'normal',
                    fontSize: '18px',
                    float: 'right',
                    fontSrc: "_www/fonts/iconfont.ttf", //wap2app内置字体文件
                    text: "\ue602",
                    onclick: function() {
                      var allPage = plus.webview.all()
                      var nowPage = plus.webview.getWebviewById('index')
                      for(var i = 0; i < allPage.length; i++) {
                        if(allPage[i].getURL() !== nowPage.getURL()) {
                          plus.webview.close(allPage[i]);
                        }
                      }
                    }
                  }],
                  splitLine: {
                    color: "#CCCCCC",
                    height: "1px"
                  }
                }
              },
              extras: {
                currentShopObj: originData.currentShopObj,
                fromTime: originData.fromTime,
                toTime: originData.toTime,
                storeReceiveList: originData.storeReceiveList,
                shopObjs: originData.shopObjs
              }
            });
          });
          mui('.receive-select').on('tap', '#shop', function() {
            mui.openWindow({
              url: 'shop-receive.html',
              id: 'shop-receive',
              styles: {
                titleNView: {
                  titleText: originData.currentShopObj.storeName + '收货',
                  titleColor: "#FFFFFF",
                  titleSize: "17px",
                  backgroundColor: "#FF6800",
                  autoBackButton: true,
                  buttons: [{
                    fontWeight: 'normal',
                    fontSize: '18px',
                    float: 'right',
                    fontSrc: "_www/fonts/iconfont.ttf", //wap2app内置字体文件
                    text: "\ue602",
                    onclick: function() {
                      var allPage = plus.webview.all()
                      var nowPage = plus.webview.getWebviewById('index')
                      for(var i = 0; i < allPage.length; i++) {
                        if(allPage[i].getURL() !== nowPage.getURL()) {
                          plus.webview.close(allPage[i]);
                        }
                      }
                    }
                  }],
                  splitLine: {
                    color: "#CCCCCC",
                    height: "1px"
                  }
                }
              },
              extras: {
                currentShopObj: originData.currentShopObj,
                fromTime: originData.fromTime,
                toTime: originData.toTime,
                shopReceiveList: originData.shopReceiveList,
                shopObjs: originData.shopObjs
              }
            });
          });
        }
      </script>
    </div>
  </body>

</html>